const app=getApp();
Page({
  data: {
    arrayList:[],
    arr:''
  },
  collected(e){
    let token=wx.getStorageSync('token');
    if(!token){
      wx.showModal({
        title:'提示',
        content:'请登录',
        success:function(res){
          if(res.confirm){
            console.log("点击了确定");
            wx.switchTab({
              url: '/pages/getuserinfo/getuserinfo',
            })
          }else if(res.cancel){
            console.log("点击取消")
            return false;
          }
        }
      })
    }
    var id=e.currentTarget.id;
    var name=this.data.arr.food_name;
    var timestamp = Date.parse(new Date());  
    timestamp = timestamp / 1000; 
    // '2021-11-29 13:23' 
    // var collection_time=
    let tm = new Date();
    var collection_time = `${tm.getFullYear()}-${tm.getMonth()+1}-${tm.getDate()} `
        + `${tm.getHours()}:${tm.getMinutes()}:${tm.getSeconds()}`;
    var open_id=wx.getStorage
    var collection_text=this.data.arr.food_material;
    var collection_image=this.data.arr.food_image;
    var anothertext=this.data.arr.food_step;
    var type='true';
    var obj={
      collection_time:collection_time,
      collection_text:collection_text,
      collection_image:collection_image,
      name:name,
      anothertext:anothertext,
      type:type
    }

    wx.request({
      url:`${app.globalData.url}/user/collected`,
      header: {
        "Content-Type": "application/json",
        authorization: wx.getStorageSync('token')
      },
      method: "POST",
      data: {open_id,collection_time,collection_text,collection_image,name,anothertext},
        success:(res)=>{
          console.log(res.data)
        },
        fail(){
          wx.showToast({
            title:'收藏失败',
            icon:'none'
          })
        }
      })
  },
  collect(id,flag){
    var islogin=wx.getStorageSync('isShow') ;
    if(islogin){
      console.log("collect")
      this.collected();
    }else{
      wx.showModal({
        title: '提示',
        content: '用户未登录！',
        success: function (res) {
          if (res.confirm) {//这里是点击了确定以后
            wx.switchTab({
              url: '/pages/getuserinfo/getuserinfo',
            })
          } else {//这里是点击了取消以后
            console.log('用户点击取消')
          }
        }
      })
  
    }
 
  },
  eatWhat(){
    var id=Math.floor(Math.random()*9);
    var arr=this.data.arrayList[id];
    this.setData({arr:arr})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.eatWhat()
  
    var _this=this;
    this.setData({
      id:options.id
      // 将navigator传递的值赋值给id
    })

    wx.request({
      url:`${app.globalData.url}/eatbook`,
      dataType:'json',
      method:'GET',
      header:{
        "content-type":'application/json'
      },
      success:res=>{
          console.log(res.data);
          this.setData({
            arrayList:res.data
            // 将获取到的数据传给初始化的变量，在后端渲染
          })
          this.eatWhat();
      },
      fail(){
        console.log("no");
      }
    })
   
  }
})